I have the same problem with NavigationLinks (in place of Buttons in the original question). Setting .buttonStyle(.borderless) or .buttonStyle(PlainButtonStyle()) does not fix it. ChatGPT gave me a clear example, which it claimed would work, but just repeats the problem
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(0..<10) { index in
HStack {
NavigationLink(destination: DestinationView1(item: index)) {
Text("Navigate to View 1")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.buttonStyle(PlainButtonStyle()) // Ensure the link only activates on its area
Spacer()
NavigationLink(destination: DestinationView2(item: index)) {
Text("Navigate to View 2")
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(8)
}
.buttonStyle(PlainButtonStyle()) // Ensure the link only activates on its area
}
.padding(.vertical, 5)
}
}
.navigationBarTitle("Navigation Links")
}
}
}
struct DestinationView1: View {
var item: Int
var body: some View {
Text("Destination View 1 for item \(item)")
.navigationBarTitle("View 1", displayMode: .inline)
}
}
struct DestinationView2: View {
var item: Int
var body: some View {
Text("Destination View 2 for item \(item)")
.navigationBarTitle("View 2", displayMode: .inline)
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: